package io.renren.modules.job.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.DateUtils;
import io.renren.modules.sp.dao.SpMatchItemDao;
import io.renren.modules.sp.dao.SpMatchItemTeamDao;
import io.renren.modules.sp.entity.SpMatchItemEntity;
import io.renren.modules.sp.entity.SpMatchItemTeamEntity;
import io.renren.modules.sp.entity.SpTeamUserEntity;
import io.renren.modules.sp.service.SpMatchItemTeamService;
import io.renren.modules.sp.service.SpTeamUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 自动解绑定时任务
 * @author Mark sunlightcs@gmail.com
 */
@Component("autoUnbindTask")
public class AutoUnbindTask implements ITask{
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private SpTeamUserService spTeamUserService;

	@Override
	public void run(String params){
		logger.debug("TestTask定时任务正在执行，参数为：{}", params);
		this.unbind();
	}

	private void unbind(){
		String  now = DateUtils.format(new Date(),"yyyy-MM-dd");
		//查询所有teamUser表待解绑的数据
		List<SpTeamUserEntity> teamUserList = spTeamUserService.queryList(
				new QueryWrapper<SpTeamUserEntity>()
				.eq("status", "1")
				//due_time 等于当天或者小于当天最后一秒
				.le("due_time", now + " 23:59:59")
		);
		for (SpTeamUserEntity teamUserEntity : teamUserList) {
			//直接删除
			spTeamUserService.delete(new Long[]{teamUserEntity.getId()});
		}
	}



}
